using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Chimera2.Classes;

namespace Chimera2.GUI
{
    public partial class Admins : Form
    {
        Admin admin = new Admin();
        List<Hub> hubs = new List<Hub>();

        // define delegate
        public delegate void PassControl(object sender);

        // create instance (null)
        public PassControl passControl;

        public Admins()
        {
        }

        public Admins(Admin _admin, Dictionary<Guid, Hub> _hubs)
        {
            InitializeComponent();
            admin = _admin;
            hubs = new List<Hub>(_hubs.Values);
            OnLoad();
        }

        private void OnLoad()
        {
            txtUserName.Text = admin.Username;

            foreach (Hub hub in hubs)
            {
                if (admin.AccessFrom.Contains(hub.Guid))
                {
                    clbAllowAccessFromList.Items.Add(hub.HubName, true);
                }
                else
                {
                    clbAllowAccessFromList.Items.Add(hub.HubName, false);
                }
            }

            chkEditHubs.Checked = admin.CanAdminHubs;
            chkEditBulletins.Checked = admin.CanAdminBulletins;
            chkEditEditors.Checked = admin.CanAdminEditors;
        }

        private void OnSave()
        {
            admin.Username = txtUserName.Text;

            admin.AccessFrom.Clear();
            foreach (int index in clbAllowAccessFromList.CheckedIndices)
            {
                admin.AccessFrom.Add(hubs[index].Guid);
            }

            admin.CanAdminHubs = chkEditHubs.Checked;
            admin.CanAdminBulletins = chkEditBulletins.Checked;
            admin.CanAdminEditors = chkEditEditors.Checked;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtUserName.Text))
            {
                MessageBox.Show("'UserName' is required.", "Admin", MessageBoxButtons.OK);
            }
            else
            {
                OnSave();
                passControl(admin);
                this.Close();
            }
        }
    }
}